﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace YXShop.Web.membercenter
{
    public partial class member_index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["MemberName"] != null)
                {
                    Response.Write(GetHtml());
                }
                else
                {
                    string url = YXShop.TemplateAction.Common.UrlStr("login", "", "membercenter/");
                    Response.Redirect(url);
                }
            }
        }
 
        private string GetHtml()
        {
            string strCoutent = YXShop.TemplateAction.Common.YXShop_PageContent("memberdefault");
            YXShop.BLL.Shop.Shop shopBll = new YXShop.BLL.Shop.Shop();
            List<YXShop.Model.Shop.Shop> list = shopBll.GetListShopByWhere("uid=" + Session["MemberID"].ToString());
            if (strCoutent.IndexOf("{$userInfo$}")>-1)
            {
                strCoutent = strCoutent.Replace("{$userInfo$}", GetMember(Convert.ToInt32(Session["MemberID"])));
            }
            if(list.Count>0)
            {
                if (strCoutent.IndexOf("[#storeId#]") > -1)
                {

                    strCoutent = strCoutent.Replace("[#storeId#]", list[0].Sid.ToString());

                }
                if (strCoutent.IndexOf("[#userId#]") > -1)
                {
                    strCoutent = strCoutent.Replace("[#userId#]", Session["MemberID"].ToString());
                }
            }          
            return strCoutent.ToString();
        }

        private string GetMember(int id)
        {
            StringBuilder sb = new StringBuilder();
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            YXShop.BLL.Member.MemberAccount accountBll = new YXShop.BLL.Member.MemberAccount();
            YXShop.Model.Member.MemberAccount member = accountBll.GetModel(id);
            YXShop.BLL.Member.MemberRank rankBll = new YXShop.BLL.Member.MemberRank();
            YXShop.BLL.Shop.Shop shopbll = new YXShop.BLL.Shop.Shop();
            if(member!=null)
            {
                YXShop.Model.Member.MemberRank rank = rankBll.GetModel(Convert.ToInt32(member.UserGroup));
                int type = Convert.ToInt32(member.UserType);
               if(rank!=null)
               {
                   string url = YXShop.TemplateAction.Common.UrlStr("exitmembercenter", "", "membercenter/");
                   switch (type)
                   {  
                       //普通会员
                       case 0:
                           sb.Append("<div class='simple_title'><label id='simple' class='simple1'>我是买家</label>");
                          List<YXShop.Model.Shop.Shop> list=shopbll.GetListShopByWhere("uid="+id+"");
                           if (sp.IsOpenShops)
                           {
                               if (list.Count > 0)
                               {
                                   sb.Append("<label id='shopm' class='shopm1'>我是卖家");
                               }
                               else
                               {
                                   string strUrl = YXShop.TemplateAction.Common.UrlStr("shop_add", "", "membercenter/shop/shop_administer/");
                                   sb.Append("<label id='shopmS' class='shopm1'>");
                                   sb.Append("<a href='" + strUrl + "' target='memberMain' class='shopm1'>申请店铺</a>");
                               }
                           }
                           sb.Append("</label>");
                           sb.Append("</div>");
                           sb.Append("<div id='buyerInfo'>");
                           sb.Append("<dl><dt>欢迎您：</dt><dd><font>" + member.UserId + "</font></dd></dl>");
                           sb.Append("<dl><dt>等级：</dt><dd>" + rank.Name +"</dd></dl>");
                           sb.Append("<dl><dt>积分：</dt><dd>" + member.Points.ToString() + "</dd></dl>");
                           sb.Append("<ul><li><a href='member_index.aspx'>进入我的主页</a></li>");
                           sb.Append("<li><a href='"+url+"'>退出管理</a></li></ul></div>");

                           if (list.Count > 0)
                           {
                               sb.Append("<div id='salerInfo'><dl><dt>欢迎您：</dt><dd><font>" + member.UserId + "</font></dd></dl>");
                               sb.Append("<dl><dt>店铺名称：</dt><dd>" + list[0].Shopname + "</dd></dl>");
                               //sb.Append("<dl><dt>店铺等级：</dt><dd>" + sModel.ShopRank + "</dd></dl>");
                               //sb.Append("<dl><dt>信誉度：</dt><dd>" + sModel.PrestigeRank + "</dd></dl>");
                               sb.Append("<ul><li><a href='member_index.aspx'>进入我的主页</a></li>");
                               sb.Append("<li><a href='"+url+"'>退出管理</a></li></ul></div>");
                           }
                           break;
                       //单位会员
                       case 1:
                           sb.Append("<div><label id='simple' class='simple1'>我是买家</label><label id='shopm' class='shopm1'>我是卖家</label></div>");
                           sb.Append("<div id='buyerInfo'>");
                           sb.Append("<dl><dt>欢迎您：</dt><dd><font>" + member.UserId + "</font></dd></dl>");
                           sb.Append("<dl><dt>等级：</dt><dd>" + rank.Name + "</dd></dl>");
                           sb.Append("<dl><dt>积分：</dt><dd>" + member.Points.ToString() + "</dd></dl>");
                           sb.Append("<ul><li><a href='member_index.aspx'>进入我的主页</a></li>");
                           sb.Append("<li><a href='"+url+"'>退出管理</a></li></ul></div>");
                           break;
                      //店铺会员
                       default:
                           break;
                   }
               }
              
            }         
            return sb.ToString();
        }
      
    }
}
